package com.ghost.alibabaghost.web.controller.sys;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ghost.alibabaghost.api.model.sys.SysRoleModel;
import com.ghost.alibabaghost.api.service.sys.SysRoleService;
import com.ghost.alibabaghost.result.SingleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/14 23:22
 */
@RestController
public class SysRoleController {
    private static final Logger logger = LoggerFactory.getLogger(SysRoleController.class);
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 查询全部角色
     *
     * @return
     */
    @RequestMapping(value = "/queryAllRole")
    public SingleResult<List<SysRoleModel>> queryAllRole() {
        logger.info("查询全部角色");
        try {
            return SingleResult.buildSuccess(sysRoleService.queryAll());
        } catch (Exception e) {
            logger.info("查询全部角色", e);
            return SingleResult.buildFailure();
        }

    }

    /**
     * 查询全部角色
     *
     * @return
     */
    @RequestMapping(value = "/queryAllRoleArray")
    public SingleResult<JSONArray> queryAllRolearray() {
        logger.info("查询全部角色");
        try {
            List<SysRoleModel> sysRoleModels = sysRoleService.queryAll();
            Object[] objects = sysRoleModels.toArray();
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(objects));
            logger.info(jsonArray.toJSONString());
            return SingleResult.buildSuccess(jsonArray);
        } catch (Exception e) {
            logger.info("查询全部角色", e);
            return SingleResult.buildFailure();
        }

    }

    /**
     * 根据主键查询角色
     *
     * @param id
     * @return
     */
    @RequestMapping("selectRoleById")
    public SingleResult<SysRoleModel> selectRoleById(@RequestParam(value = "id") Integer id) {
        logger.info("根据主键查询角色-->param:id=" + id);
        return SingleResult.buildSuccess(sysRoleService.selectRoleById(id));
    }

    /**
     * 根据主键删除角色
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteRoleById")
    public SingleResult<String> deleteRoleById(@RequestParam(value = "id") Integer id) {
        logger.info("根据主键删除角色-->param:id=" + id);
        Integer integer = sysRoleService.deleteRoleById(id);
        if (integer > 0) return SingleResult.buildSuccess("删除成功");
        return SingleResult.buildFailure();
    }

    /**
     * 新增角色
     *
     * @param sysRoleModel
     * @return
     */
    @RequestMapping("saveRole")
    public SingleResult<String> saveRole(@RequestBody SysRoleModel sysRoleModel) {
        logger.info("新增角色-->param:sysRoleModel=" + sysRoleModel.toString());
        try {
            sysRoleService.saveRole(sysRoleModel);
            return SingleResult.buildSuccess("新增成功！");
        } catch (Exception e) {
            logger.error("新增角色", e);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 更新角色
     *
     * @param sysRoleModel
     * @return
     */
    @RequestMapping("updateRole")
    public SingleResult<String> updateRole(@RequestBody SysRoleModel sysRoleModel) {
        logger.info("更新角色-->param:sysRoleModel=" + sysRoleModel.toString());
        try {
            sysRoleService.updateRole(sysRoleModel);
            return SingleResult.buildSuccess("更新成功！");
        } catch (Exception e) {
            logger.error("更新角色", e);
            return SingleResult.buildFailure();
        }
    }
}
